<template>
  <div class="">
      <el-button @click="pulish" type="success" size="small" class="w150 mb20">添加弹幕</el-button>
        <el-table :data="tableData" border style="width: 100%" header-row-class-name="bgd8">
            <el-table-column  label="序号" type="index"> </el-table-column>
            <el-table-column prop="content" label="弹幕名称" > </el-table-column>
            <el-table-column prop="contentEn" label="弹幕英文名称" > </el-table-column>
            <el-table-column prop="type" label="所属模块">
                <template scope="scope">
                            <span v-if="scope.row.type == 1">合作模式</span>
                            <span v-if="scope.row.type == 2">联系我们</span>
                </template>
            </el-table-column>
            <el-table-column label="发布日期">
              <template slot-scope="scope">
                  <span>{{scope.row.createTime|formatDate}}</span>
                </template>
            </el-table-column>
            <el-table-column label="操作 ">
                <template slot-scope="scope">
                    <el-button @click="edit(scope.row)" type="info" size="small">编辑</el-button>
                    <el-button type="danger" size="small"  @click="del(scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination background  layout="prev, pager, next" :total="totalCount" @current-change="currentChange" @prev-click="prevClick" @next-click="nextClick"> </el-pagination>
        <el-dialog title="添加弹幕" class="w750" :visible.sync="dialogTableVisible">
          <el-form ref="ruleForm" :rules='rules' :inline="true" :model="formData" class="msgform newstypeform" label-width="120px">
            <el-form-item label="弹幕名称" prop="name" class="formblock is-required" >
              <el-input class="w270" v-model="formData.content"></el-input>
            </el-form-item>
             <el-form-item label="弹幕英文名称" prop="nameEn" class="is-required">
              <el-input class="w270" v-model="formData.contentEn"></el-input>
            </el-form-item>
            <el-form-item label="所属模块" class="formblock is-required">
                <el-select class="w270"  v-model="formData.type">
                <el-option label="合作模式" value="1"></el-option>
                <el-option label="联系我们" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-button type="primary" class="w140" size="small" @click="onSubmit">确认</el-button>
          </el-form>
        </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  data () {
    return {
        dialogTableVisible:false,
        dialogTableVisible_content:false,
        formData:{
          content:'',
          contentEn:'',
          type:''
        },
        tableData: [],
        curPage:1,
        totalCount:0,
        rules:{
          content: [{ required: true, message: '请输入弹幕名称', trigger: 'blur' }], 
          contentEn: [{ required: true, message: '请输入弹幕英文名称', trigger: 'blur' }],       
        }
    }
  },
  filters:{
    formatDate(date){
      date=new Date(date);
      var year=date.getFullYear();
      var month=date.getMonth()+1;
      var day=date.getDate();
      if(month<10){
        month='0'+month;
      }
      return year+'-'+month+'-'+day;
    }
  },
  methods:{
      del(data){
        this.$confirm('确定要删除吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          var id =data.id;
          var httpApi=this.$store.state.httpApi;
          this.$http.post(this.$store.state.httpApi + 'blFrameInfo/del?id='+id).then(response =>{
            if(response.status == 200){
              this.$message.success('删除成功');
					    this.getRows();
            }else{
              console.log('接口返回失败!',response);
            }
          },response => {          
            console.log('请求失败',response)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      },
      edit(data){
         if(this.$refs.ruleForm){
            this.$refs.ruleForm.resetFields();
          }
          this.dialogTableVisible=true;
          this.formData.id = data.id;
          this.formData.type = data.type;
          this.formData.content = data.content;
          this.formData.contentEn = data.contentEn;
      },
      pulish(){
          this.formData.type = null;
          this.formData.content = "";
          this.formData.contentEn = "";
          this.dialogTableVisible=true;
         
      },
      onSubmit(){  
           var flag=true;
           for (var j in this.formData){    
              if(this.formData[j]=='' || this.formData[j]==null){
              flag=false;
            }
           } 
           if(flag){
              this.$http.post(this.$store.state.httpApi + (this.formData.id ?'blFrameInfo/update':'blFrameInfo/save'),this.formData).then(response =>{
              if(response.status == 200){
                this.$message.success(this.formData.id ? '修改成功' : '发布成功');
                this.dialogTableVisible=false;
                this.getRows();
              }else{
                console.log('接口返回失败!',response);
              }
              },response => {
                console.log('请求失败',response)
              })
           }else{
              this.$message.error('您还有必填项还没有填！');
           }
           
      },
      showContent(data){
        this.dialogTableVisible_content=true;
      },
      currentChange(page){
          this.curPage = page;
          this.getRows();
      },
      //上一页
      prevClick(page){
          this.curPage = page;
          this.getRows();
      },
    //  下一页
      nextClick(page){
          this.curPage = page;
          this.getRows();
      },
      getRows(){
          this.$http.get(this.$store.state.httpApi + 'blFrameInfo/page?currentPage='+this.curPage+'&pageNumber='+10).then(response =>{
          if(response.status == 200){
          this.tableData=response.data.data.pp;
          this.totalCount = response.data.data.page.totalPage*10;
          }else{
            console.log('接口返回失败!',response);
          }
          },response => {
            console.log('请求失败',response)
          })
      },
  },
  created(){
    this.getRows();
  }

}
</script>
<style>
    .h200 .el-textarea__inner{
        height:200px
    } 
</style>
<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
    .mr10{
        margin-right:10px;
    }
    .msgform{
        width:570px;
        margin:0 auto;
    }
    .el-form--inline .el-form-item{
      margin-right:0;
    }
    .w140{
      margin-top:30px;
    }
</style>
